SlideShare a Scribd company logo
Class No.06  Data Structures http://ecomputernotes.com
Stack Using Linked List We can avoid the size limitation of a stack implemented with an array by using a linked list to hold the stack elements. As with array, however, we need to decide where to insert elements in the list and where to delete them so that push and pop will run the fastest. http://ecomputernotes.com
Stack Using Linked List For a singly-linked list, insert at start or end takes constant time using the head and current pointers respectively. Removing an element at the start is constant time but removal at the end required traversing the list to the node one before the last. Make sense to place stack elements at the start of the list because insert and removal are constant time. http://ecomputernotes.com
Stack Using Linked List No need for the current pointer; head is enough.  top 2 5 7 1 1 7 5 2 head http://ecomputernotes.com
Stack Operation: List int pop() { int x = head->get(); Node* p = head; head = head->getNext(); delete p; return x; } top 2 5 7 1 7 5 2 head http://ecomputernotes.com
Stack Operation: List void push(int x) { Node* newNode = new Node(); newNode->set(x); newNode->setNext(head); head = newNode; } top 2 5 7 9 7 5 2 head push(9) 9 newNode http://ecomputernotes.com
Stack Operation: List int top() { return head->get(); }  int IsEmpty() { return ( head == NULL ); } All four operations take constant time. http://ecomputernotes.com
Stack: Array or List Since both implementations support stack operations in constant time, any reason to choose one over the other? Allocating and deallocating memory for list nodes does take more time than preallocated array. List uses only as much memory as required by the nodes; array requires allocation ahead of time. List pointers (head, next) require extra memory. Array has an upper limit; List is limited by dynamic memory allocation. http://ecomputernotes.com
Use of Stack Example of use: prefix, infix, postfix expressions. Consider the expression A+B: we think of applying the  operator  “+” to the  operands  A and B. “ +” is termed a  binary operator : it takes two operands. Writing the sum as A+B is called the  infix  form of the expression.  http://ecomputernotes.com
Prefix, Infix, Postfix Two other ways of writing the expression are + A B  prefix A B +  postfix The prefixes “pre” and “post” refer to the position of the operator with respect to the two operands.  http://ecomputernotes.com
Prefix, Infix, Postfix Consider the infix expression A + B * C We “know” that multiplication is done before addition. The expression is interpreted as  A + ( B * C ) Multiplication has  precedence  over addition. http://ecomputernotes.com
Prefix, Infix, Postfix Conversion to postfix A + ( B * C ) infix form http://ecomputernotes.com
Prefix, Infix, Postfix Conversion to postfix A + ( B * C ) infix form A + ( B C * ) convert multiplication http://ecomputernotes.com
Prefix, Infix, Postfix Conversion to postfix A + ( B * C ) infix form A + ( B C * ) convert multiplication A ( B C * ) + convert addition http://ecomputernotes.com
Prefix, Infix, Postfix Conversion to postfix A + ( B * C ) infix form A + ( B C * ) convert multiplication A ( B C * ) + convert addition A B C * + postfix form http://ecomputernotes.com
Prefix, Infix, Postfix Conversion to postfix (A + B ) * C infix form http://ecomputernotes.com
Prefix, Infix, Postfix Conversion to postfix (A + B ) * C infix form ( A B + ) * C convert addition http://ecomputernotes.com
Prefix, Infix, Postfix Conversion to postfix (A + B ) * C infix form ( A B + ) * C convert addition ( A B + ) C * convert multiplication http://ecomputernotes.com
Prefix, Infix, Postfix Conversion to postfix (A + B ) * C infix form ( A B + ) * C convert addition ( A B + ) C * convert multiplication A B + C * postfix form http://ecomputernotes.com
Precedence of Operators The five binary operators are: addition, subtraction, multiplication, division and exponentiation. The order of precedence is (highest to lowest) Exponentiation  Multiplication/division *, / Addition/subtraction +, - http://ecomputernotes.com
Precedence of Operators For operators of same precedence, the left-to-right rule applies: A+B+C means (A+B)+C. For exponentiation, the right-to-left rule applies A  B  C means A  ( B  C ) http://ecomputernotes.com
Infix to Postfix Infix Postfix A + B A B + 12 + 60 – 23 12 60 + 23 – (A + B)*(C – D ) A B + C D – * A  B * C – D + E/F A B  C*D – E F/+ http://ecomputernotes.com

More Related Content

PPT
Stack linked list
PPTX
Conversion of Infix to Prefix and Postfix with Stack
PDF
PPTX
Data structure Stack
PPTX
Variadic functions
PPTX
Command line arguments
PPT
Conversion of Infix To Postfix Expressions
PPT
Stack linked list
Conversion of Infix to Prefix and Postfix with Stack
Data structure Stack
Variadic functions
Command line arguments
Conversion of Infix To Postfix Expressions

What's hot (20)

PPTX
Stack data structure
PPTX
Stack and queue
PPT
Stack application
PPSX
PPTX
Queue oop
PPT
PPTX
Stacks in c++
PPT
Computer notes - Hashing
PPT
computer notes - Data Structures - 35
PPT
MEngine Overview
PPTX
Unit 3 stack
PPTX
Stack Data structure
PPT
Stack Data Structure & It's Application
PPT
Stack Operation In Data Structure
PPTX
Presentation topic is stick data structure
PPT
An adaptive algorithm for detection of duplicate records
PPTX
Application of Stack - Yadraj Meena
PPTX
Introduction to stack
Stack data structure
Stack and queue
Stack application
Queue oop
Stacks in c++
Computer notes - Hashing
computer notes - Data Structures - 35
MEngine Overview
Unit 3 stack
Stack Data structure
Stack Data Structure & It's Application
Stack Operation In Data Structure
Presentation topic is stick data structure
An adaptive algorithm for detection of duplicate records
Application of Stack - Yadraj Meena
Introduction to stack
Ad

Viewers also liked (20)

PPT
computer notes - Data Structures - 29
PPT
computer notes - Data Structures - 2
PPT
computer notes - Data Structures - 9
PPT
computer notes - Data Structures - 38
PPT
computer notes - Data Structures - 22
PDF
computer notes - Deleting a node
PPT
computer notes - Data Structures - 23
PPT
computer notes - Data Structures - 16
PPT
computer notes - Data Structures - 24
PPT
computer notes - Data Structures - 15
PPT
computer notes - Data Structures - 13
PPT
computer notes - Data Structures - 10
PPT
computer notes - Data Structures - 3
PPT
computer notes - Data Structures - 33
PPT
computer notes - Data Structures - 20
PPT
computer notes - Data Structures - 12
PPT
computer notes - Data Structures - 32
PPT
computer notes - Data Structures - 27
PPT
computer notes - Data Structures - 7
PPT
computer notes - Data Structures - 28
computer notes - Data Structures - 29
computer notes - Data Structures - 2
computer notes - Data Structures - 9
computer notes - Data Structures - 38
computer notes - Data Structures - 22
computer notes - Deleting a node
computer notes - Data Structures - 23
computer notes - Data Structures - 16
computer notes - Data Structures - 24
computer notes - Data Structures - 15
computer notes - Data Structures - 13
computer notes - Data Structures - 10
computer notes - Data Structures - 3
computer notes - Data Structures - 33
computer notes - Data Structures - 20
computer notes - Data Structures - 12
computer notes - Data Structures - 32
computer notes - Data Structures - 27
computer notes - Data Structures - 7
computer notes - Data Structures - 28
Ad

Similar to computer notes - Data Structures - 6 (20)

PPT
Lecture6
PPTX
Data Structure and Algorithms by Sabeen Memon03.pptx
PPTX
Data structures
PPTX
PPT Lecture 3.2.1 stack newxxxxxxxxxx.pptx
PPTX
2.2 stack applications Infix to Postfix & Evaluation of Post Fix
PPTX
STACK Applications in DS
PDF
Data structures stacks
PPTX
c programming and data structure notes for ECE
PDF
DOCX
Mycasestudy
PDF
Data Structures And Algorithms(stacks queues)
PPTX
DS MOD2 (1) (1).pptx
PPTX
Stack in Sata Structure
PDF
Applications of Stack (Data Structure).pdf
PPTX
Stacks and queues using aaray line .pptx
PPTX
13 Stacks and Queues.pptx
PPTX
Unit 3 Stacks and Queues.pptx
PDF
Stacks,queues,linked-list
PPT
Stack ppt file of Stack DSA For lab in the lab of DSA lecture and Lab.ppt
PPT
358 33 powerpoint-slides_9-stacks-queues_chapter-9
Lecture6
Data Structure and Algorithms by Sabeen Memon03.pptx
Data structures
PPT Lecture 3.2.1 stack newxxxxxxxxxx.pptx
2.2 stack applications Infix to Postfix & Evaluation of Post Fix
STACK Applications in DS
Data structures stacks
c programming and data structure notes for ECE
Mycasestudy
Data Structures And Algorithms(stacks queues)
DS MOD2 (1) (1).pptx
Stack in Sata Structure
Applications of Stack (Data Structure).pdf
Stacks and queues using aaray line .pptx
13 Stacks and Queues.pptx
Unit 3 Stacks and Queues.pptx
Stacks,queues,linked-list
Stack ppt file of Stack DSA For lab in the lab of DSA lecture and Lab.ppt
358 33 powerpoint-slides_9-stacks-queues_chapter-9

More from ecomputernotes (20)

PPT
computer notes - Data Structures - 30
PPT
computer notes - Data Structures - 39
PPT
computer notes - Data Structures - 11
DOC
Computer notes - Including Constraints
DOC
Computer notes - Date time Functions
DOC
Computer notes - Subqueries
DOC
Computer notes - Other Database Objects
PPT
computer notes - Data Structures - 19
PPT
computer notes - Data Structures - 31
PPT
computer notes - Data Structures - 4
DOC
Computer notes - Advanced Subqueries
DOC
Computer notes - Aggregating Data Using Group Functions
PPT
computer notes - Data Structures - 36
DOC
Computer notes - Enhancements to the GROUP BY Clause
DOC
Computer notes - Manipulating Data
DOC
Computer notes - Writing Basic SQL SELECT Statements
PPT
computer notes - Data Structures - 14
PPT
computer notes - Data Structures - 5
DOC
Computer notes - Controlling User Access
DOC
Computer notes - Using SET Operator
computer notes - Data Structures - 30
computer notes - Data Structures - 39
computer notes - Data Structures - 11
Computer notes - Including Constraints
Computer notes - Date time Functions
Computer notes - Subqueries
Computer notes - Other Database Objects
computer notes - Data Structures - 19
computer notes - Data Structures - 31
computer notes - Data Structures - 4
Computer notes - Advanced Subqueries
Computer notes - Aggregating Data Using Group Functions
computer notes - Data Structures - 36
Computer notes - Enhancements to the GROUP BY Clause
Computer notes - Manipulating Data
Computer notes - Writing Basic SQL SELECT Statements
computer notes - Data Structures - 14
computer notes - Data Structures - 5
Computer notes - Controlling User Access
Computer notes - Using SET Operator

Recently uploaded (20)

DOCX
The AUB Centre for AI in Media Proposal.docx
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PPTX
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
PPT
Teaching material agriculture food technology
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PPTX
Spectroscopy.pptx food analysis technology
PDF
NewMind AI Weekly Chronicles - August'25 Week I
PDF
Unlocking AI with Model Context Protocol (MCP)
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PDF
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PDF
Encapsulation theory and applications.pdf
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PPTX
Big Data Technologies - Introduction.pptx
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PDF
Encapsulation_ Review paper, used for researhc scholars
The AUB Centre for AI in Media Proposal.docx
“AI and Expert System Decision Support & Business Intelligence Systems”
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
Teaching material agriculture food technology
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
Diabetes mellitus diagnosis method based random forest with bat algorithm
Spectroscopy.pptx food analysis technology
NewMind AI Weekly Chronicles - August'25 Week I
Unlocking AI with Model Context Protocol (MCP)
Per capita expenditure prediction using model stacking based on satellite ima...
Chapter 3 Spatial Domain Image Processing.pdf
Agricultural_Statistics_at_a_Glance_2022_0.pdf
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
Digital-Transformation-Roadmap-for-Companies.pptx
Encapsulation theory and applications.pdf
Dropbox Q2 2025 Financial Results & Investor Presentation
Big Data Technologies - Introduction.pptx
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
Encapsulation_ Review paper, used for researhc scholars

computer notes - Data Structures - 6

  • 1. Class No.06 Data Structures http://ecomputernotes.com
  • 2. Stack Using Linked List We can avoid the size limitation of a stack implemented with an array by using a linked list to hold the stack elements. As with array, however, we need to decide where to insert elements in the list and where to delete them so that push and pop will run the fastest. http://ecomputernotes.com
  • 3. Stack Using Linked List For a singly-linked list, insert at start or end takes constant time using the head and current pointers respectively. Removing an element at the start is constant time but removal at the end required traversing the list to the node one before the last. Make sense to place stack elements at the start of the list because insert and removal are constant time. http://ecomputernotes.com
  • 4. Stack Using Linked List No need for the current pointer; head is enough. top 2 5 7 1 1 7 5 2 head http://ecomputernotes.com
  • 5. Stack Operation: List int pop() { int x = head->get(); Node* p = head; head = head->getNext(); delete p; return x; } top 2 5 7 1 7 5 2 head http://ecomputernotes.com
  • 6. Stack Operation: List void push(int x) { Node* newNode = new Node(); newNode->set(x); newNode->setNext(head); head = newNode; } top 2 5 7 9 7 5 2 head push(9) 9 newNode http://ecomputernotes.com
  • 7. Stack Operation: List int top() { return head->get(); } int IsEmpty() { return ( head == NULL ); } All four operations take constant time. http://ecomputernotes.com
  • 8. Stack: Array or List Since both implementations support stack operations in constant time, any reason to choose one over the other? Allocating and deallocating memory for list nodes does take more time than preallocated array. List uses only as much memory as required by the nodes; array requires allocation ahead of time. List pointers (head, next) require extra memory. Array has an upper limit; List is limited by dynamic memory allocation. http://ecomputernotes.com
  • 9. Use of Stack Example of use: prefix, infix, postfix expressions. Consider the expression A+B: we think of applying the operator “+” to the operands A and B. “ +” is termed a binary operator : it takes two operands. Writing the sum as A+B is called the infix form of the expression. http://ecomputernotes.com
  • 10. Prefix, Infix, Postfix Two other ways of writing the expression are + A B prefix A B + postfix The prefixes “pre” and “post” refer to the position of the operator with respect to the two operands. http://ecomputernotes.com
  • 11. Prefix, Infix, Postfix Consider the infix expression A + B * C We “know” that multiplication is done before addition. The expression is interpreted as A + ( B * C ) Multiplication has precedence over addition. http://ecomputernotes.com
  • 12. Prefix, Infix, Postfix Conversion to postfix A + ( B * C ) infix form http://ecomputernotes.com
  • 13. Prefix, Infix, Postfix Conversion to postfix A + ( B * C ) infix form A + ( B C * ) convert multiplication http://ecomputernotes.com
  • 14. Prefix, Infix, Postfix Conversion to postfix A + ( B * C ) infix form A + ( B C * ) convert multiplication A ( B C * ) + convert addition http://ecomputernotes.com
  • 15. Prefix, Infix, Postfix Conversion to postfix A + ( B * C ) infix form A + ( B C * ) convert multiplication A ( B C * ) + convert addition A B C * + postfix form http://ecomputernotes.com
  • 16. Prefix, Infix, Postfix Conversion to postfix (A + B ) * C infix form http://ecomputernotes.com
  • 17. Prefix, Infix, Postfix Conversion to postfix (A + B ) * C infix form ( A B + ) * C convert addition http://ecomputernotes.com
  • 18. Prefix, Infix, Postfix Conversion to postfix (A + B ) * C infix form ( A B + ) * C convert addition ( A B + ) C * convert multiplication http://ecomputernotes.com
  • 19. Prefix, Infix, Postfix Conversion to postfix (A + B ) * C infix form ( A B + ) * C convert addition ( A B + ) C * convert multiplication A B + C * postfix form http://ecomputernotes.com
  • 20. Precedence of Operators The five binary operators are: addition, subtraction, multiplication, division and exponentiation. The order of precedence is (highest to lowest) Exponentiation  Multiplication/division *, / Addition/subtraction +, - http://ecomputernotes.com
  • 21. Precedence of Operators For operators of same precedence, the left-to-right rule applies: A+B+C means (A+B)+C. For exponentiation, the right-to-left rule applies A  B  C means A  ( B  C ) http://ecomputernotes.com
  • 22. Infix to Postfix Infix Postfix A + B A B + 12 + 60 – 23 12 60 + 23 – (A + B)*(C – D ) A B + C D – * A  B * C – D + E/F A B  C*D – E F/+ http://ecomputernotes.com

Editor's Notes

  • #3: End of lecture 5 You might believe that faster computers make it unnecessary to be concerned with efficiency. However… So we need special training.
  • #4: A primary concern for this course is efficiency. You might believe that faster computers make it unnecessary to be concerned with efficiency. However… So we need special training.
  • #5: A primary concern for this course is efficiency. You might believe that faster computers make it unnecessary to be concerned with efficiency. However… So we need special training.
  • #6: A primary concern for this course is efficiency. You might believe that faster computers make it unnecessary to be concerned with efficiency. However… So we need special training.
  • #7: A primary concern for this course is efficiency. You might believe that faster computers make it unnecessary to be concerned with efficiency. However… So we need special training.
  • #8: A primary concern for this course is efficiency. You might believe that faster computers make it unnecessary to be concerned with efficiency. However… So we need special training.
  • #9: A primary concern for this course is efficiency. You might believe that faster computers make it unnecessary to be concerned with efficiency. However… So we need special training.
  • #10: A primary concern for this course is efficiency. You might believe that faster computers make it unnecessary to be concerned with efficiency. However… So we need special training.
  • #11: A primary concern for this course is efficiency. You might believe that faster computers make it unnecessary to be concerned with efficiency. However… So we need special training.
  • #12: A primary concern for this course is efficiency. You might believe that faster computers make it unnecessary to be concerned with efficiency. However… So we need special training.
  • #13: A primary concern for this course is efficiency. You might believe that faster computers make it unnecessary to be concerned with efficiency. However… So we need special training.
  • #14: A primary concern for this course is efficiency. You might believe that faster computers make it unnecessary to be concerned with efficiency. However… So we need special training.
  • #15: A primary concern for this course is efficiency. You might believe that faster computers make it unnecessary to be concerned with efficiency. However… So we need special training.
  • #16: A primary concern for this course is efficiency. You might believe that faster computers make it unnecessary to be concerned with efficiency. However… So we need special training.
  • #17: A primary concern for this course is efficiency. You might believe that faster computers make it unnecessary to be concerned with efficiency. However… So we need special training.
  • #18: A primary concern for this course is efficiency. You might believe that faster computers make it unnecessary to be concerned with efficiency. However… So we need special training.
  • #19: A primary concern for this course is efficiency. You might believe that faster computers make it unnecessary to be concerned with efficiency. However… So we need special training.
  • #20: A primary concern for this course is efficiency. You might believe that faster computers make it unnecessary to be concerned with efficiency. However… So we need special training.
  • #21: A primary concern for this course is efficiency. You might believe that faster computers make it unnecessary to be concerned with efficiency. However… So we need special training.
  • #22: A primary concern for this course is efficiency. You might believe that faster computers make it unnecessary to be concerned with efficiency. However… So we need special training.
  • #23: End of lecture 6